package unipd.esp1112.pro;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
 * 
 * Activity where the user can set the limit of the training he is going to start
 * 
 * @author andrea basso, federico lancerin, alex tomasello
 * 
 */
public class ImpostaParametri extends Activity {

	/**
	 * auxiliary variable called from another activity to kill this one and go directly back to the main menu
	 */
	public static boolean daTerminare = false;
	/**
	 * textview that displays the kind of limit the user is setting
	 */
	private TextView descrizioneLimite;
	/**
	 * edittext where the user can input the desired numerical value of the limit
	 */
	private EditText valoreImpostato;
	/**
	 * button to proceed to the next activity
	 */
	private Button continua;
	/**
	 * variable used to store the value of the limit and check its coherence
	 */
	private String valore;
	/**
	 * variable to store the name of the profile the user is currently using
	 */
	private String nomeProfilo = null;
	/**
	 * training type: 'TEMPO' , 'DISTANZA' , 'CALORIE '
	 */
	private String tipoAttivita = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setparameters);

		descrizioneLimite = (TextView) findViewById(R.id.impostaTempo);
		valoreImpostato = (EditText) findViewById(R.id.editTempo);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

		Intent intent = getIntent();
		tipoAttivita = intent.getStringExtra("attivita");
		nomeProfilo = intent.getStringExtra("profilo");

		if (tipoAttivita.equalsIgnoreCase("tempo")) {
		}

		if (tipoAttivita.equalsIgnoreCase("distanza")) {
			descrizioneLimite.setText("Imposta la distanza in metri :");
		}

		if (tipoAttivita.equalsIgnoreCase("calorie")) {
			descrizioneLimite.setText("Imposta le chilocalorie :");
		}

		continua = (Button) findViewById(R.id.continuaParametri);
		continua.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				valore = valoreImpostato.getText().toString();
				if (tipoAttivita.equalsIgnoreCase("tempo")
						&& (valore.equalsIgnoreCase("") || (Long.parseLong(valore) > 1440) || (Long.parseLong(valore) < 1)))
					descrizioneLimite.setTextColor(Color.RED);
				else if (tipoAttivita.equalsIgnoreCase("distanza")
						&& (valore.equalsIgnoreCase("") || (Long.parseLong(valore) > 100000) || (Long.parseLong(valore) < 10)))
					descrizioneLimite.setTextColor(Color.RED);
				else if (tipoAttivita.equalsIgnoreCase("calorie")
						&& (valore.equalsIgnoreCase("") || (Long.parseLong(valore) > 1000000) || (Long.parseLong(valore) < 1)))
					descrizioneLimite.setTextColor(Color.RED);
				else {
					Intent intent = new Intent(getApplicationContext(), Misurazioni.class);

					intent.putExtra("profilo", nomeProfilo);
					intent.putExtra("attivita", tipoAttivita);
					if (tipoAttivita.equalsIgnoreCase("tempo"))
						intent.putExtra("limite", "" + ((Long.parseLong(valore)) * 60));
					else
						intent.putExtra("limite", "" + Long.parseLong(valore));
					startActivity(intent);
					finish();
				}
			}
		});
	}
}
